package com.hankol.winecellar.utils;

import android.content.Context;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.AdapterListUpdateCallback;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;

import java.util.ArrayList;
import java.util.List;

/************************************
 ***        @author zhanglijie       ***
 ***        @date 2020-02-19         ***
 ***                              ***
 ************************************
 */
public abstract class MyAdapter<T> extends BaseAdapter<MyAdapter.ViewHolder> {


    private final int DEFAULT_PAGE_NUMBER = 1;
    /**
     * 列表数据
     */
    private List<T> mDataSet;
    /**
     * 当前列表的页面，默认为第一页，用于分页和加载功能
     */
    private int mPageNumber = 1;

    /**
     * 是否是最后一页，默认未 false，用于分页加载功能
     */
    private boolean mLastPage;

    /**
     * 标记对象
     */
    private Object mTag;

    private final AsyncListDiffer<T> mDiffer;

    public MyAdapter(@NonNull Context mContext) {
        super(mContext);
        mDiffer = null;
    }

    public MyAdapter(@NonNull Context mContext, @NonNull DiffUtil.ItemCallback<T> diffCallback) {
        super(mContext);
        mDiffer = new AsyncListDiffer<>(new AdapterListUpdateCallback(this),
                new AsyncDifferConfig.Builder<>(diffCallback).build());
        AsyncListDiffer.ListListener<T> mListener = MyAdapter.this::onCurrentListChanged;
        mDiffer.addListListener(mListener);

    }

    public void onCurrentListChanged(@NonNull List<T> previousList, @NonNull List<T> currentList) {

    }

    @Override
    public int getItemCount() {
        return mDataSet == null ? 0 : mDataSet.size();
    }

    public List<T> getData() {
        return mDataSet;
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setData(@NonNull List<T> data) {
        this.mDataSet = data;
        addPageNumber();
        notifyDataSetChanged();
    }

    /**
     * 添加数据，如果传入的对象为空
     *
     * @param data
     */
    public void addData(@NonNull List<T> data) {
        if (data.size() == 0) {
            return;
        }
        if (mDataSet == null || mDataSet.size() == 0 || mPageNumber == DEFAULT_PAGE_NUMBER) {
            setData(data);
        } else {
            mDataSet.addAll(data);
            addPageNumber();
            notifyItemRangeInserted(mDataSet.size() - data.size(), data.size());
        }
    }


    public void clearData() {
        if (mDataSet == null || mDataSet.size() == 0) {
            return;
        }
        mDataSet.clear();
        resetPageNumber();
        notifyDataSetChanged();
    }

    public T getItem(@IntRange(from = 0) int position) {
        return mDataSet.get(position);
    }

    public void setItem(@IntRange(from = 0) int position, @Nullable T item) {
        if (mDataSet == null) {
            mDataSet = new ArrayList<>();
        }
        mDataSet.set(position, item);
        notifyItemChanged(position);
    }

    public void addItem(@NonNull T item) {
        if (mDataSet == null) {
            mDataSet = new ArrayList<>();
        }
        addItem(mDataSet.size(), item);
    }

    public void addItem(@IntRange(from = 0) int position, @NonNull T item) {
        if (mDataSet == null) {
            mDataSet = new ArrayList<>();
        }

        if (position < mDataSet.size()) {
            mDataSet.add(position, item);
        } else {
            mDataSet.add(item);
            position = mDataSet.size() - 1;
        }
        notifyItemInserted(position);
    }

    public void removeItem(@NonNull T item) {
        int index = mDataSet.indexOf(item);
        if (index != -1) {
            removeItem(index);
        }
    }

    public void removeItem(@IntRange(from = 0) int position) {
        // 如果是在 for 循环删除后要记录 i--
        mDataSet.remove(position);
        // 告诉适配器删除数据的位置，会有动画效果
        notifyItemRemoved(position);
//        notifyItemRangeChanged(position, mDataSet.size() - position);
    }

    public int getPageNumber() {
        return mPageNumber;
    }

    public void addPageNumber() {
        mPageNumber++;
    }

    public void resetPageNumber() {
        mPageNumber = DEFAULT_PAGE_NUMBER;
    }

    public boolean isLastPage() {
        return mLastPage;
    }

    public void setLastPage(boolean flag) {
        mLastPage = flag;
    }

    public Object getTag() {
        return mTag;
    }

    public void setTag(@Nullable Object tag) {
        mTag = tag;
    }

    public abstract class ViewHolder extends BaseAdapter.ViewHolder {


        protected ViewHolder(@LayoutRes int id) {
            super(id);
        }

        protected ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


}
